// stores/counter.js
import { defineStore } from "pinia";
import api from "@/commons/api";

export const useUserStore = defineStore("user", {
  state: () => {
    return { userInfo: null, accessToken: "", refreshToken: "" };
  },
  actions: {
    login(data) {
      return new Promise((resolve, reject) => {
        api
          .login(data)
          .then((res) => {
            this.accessToken = res.accessToken;
            uni.$sc.storage.set("accessToken", res.accessToken);
            this.refreshToken = res.refreshToken;
            uni.$sc.storage.set("refreshToken", res.refreshToken);
            resolve(res);
          })
          .catch((err) => {
            console.log("🚀 login err", err);
            reject(err);
          });
      });
    },
    Logout() {
      this.userInfo = null;
      this.token = "";
    },
  },
});
